package com.shs.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shs.sys.common.DataGridView;
import com.shs.sys.common.ResultObj;
import com.shs.sys.common.WebUtils;
import com.shs.sys.entity.Loginfo;
import com.shs.sys.entity.Notice;
import com.shs.sys.entity.User;
import com.shs.sys.mapper.NoticeMapper;
import com.shs.sys.service.NoticeService;
import com.shs.sys.vo.LoginfoVo;
import com.shs.sys.vo.NoticeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jxm
 * @since 2020-04-10
 */
@RestController
@RequestMapping("notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 查询公告
     * @return
     */
    @RequestMapping("loadAllNotice")
    public DataGridView loadAllNotice(NoticeVo vo){
        Page<Notice> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Notice> wrapper = new QueryWrapper();
        wrapper.like(StringUtils.isNotBlank(vo.getTitle()),"title",vo.getTitle());
        wrapper.like(StringUtils.isNotBlank(vo.getOpername()),"opername",vo.getOpername());
        //大于
        wrapper.ge(vo.getStartTime()!=null,"logintime",vo.getStartTime());
        //小于
        wrapper.le(vo.getEndTime()!=null,"logintime",vo.getEndTime());
        wrapper.orderByDesc("createtime");
        noticeService.page(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());

    }

    /**
     * 添加公告
     * @param vo
     * @return
     */
    @RequestMapping("addNotice")
    public ResultObj addNotice(NoticeVo vo){
        try {
            vo.setCreatetime(new Date());
            User user = (User)WebUtils.getSession().getAttribute("user");
            vo.setOpername(user.getName());
            noticeService.save(vo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改公告
     * @param vo
     * @return
     */
    @RequestMapping("updateNotice")
    public ResultObj updateNotice(NoticeVo vo){
        try {
            noticeService.updateById(vo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("deleteNotice")
    public ResultObj deleteNotice(Integer id){
        try {
            noticeService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     * @param vo
     * @return
     */
    @RequestMapping("batchDeleteNotice")
    public ResultObj batchDeleteNotice(NoticeVo vo){
        List<Integer> idsList = new ArrayList<>();
        try {
            for (Integer id : vo.getIds()) {
                idsList.add(id);
            }
            noticeService.removeByIds(idsList);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }


}
